﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;
using System.Data;

public partial class Doanh_Nghiep : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["Id"] != null)
            {
                string idLoaiMH = Request.Params["Id"];
                string tenLoaiMH = Request.Params["TenLoaiMatHang"];                
                // Thuc hien Bind du lieu cho cac mat hang cua LoaiMatHang do
                Bind_Data_LoaiMatHang();
                Bind_Data_MatHang_DK(Int32.Parse(idLoaiMH), Pager1.CurrentIndex);
            }
            else
            {
                Bind_Data_LoaiMatHang();
                Bind_Data_MatHang(0, 0, Pager1.CurrentIndex);
                ltrTieude.Text = "Doanh Nghiệp";
            }
        }
    }

    /// <summary>
    /// Bindding du lieu cac LoaiMatHang thanh cac tuy chon Menu hien thi o trang
    /// Doanh-Nghiep.aspx
    /// </summary>
    protected void Bind_Data_LoaiMatHang()
    {
        int ToTalRows = 0;
        string WhereClause = "1=1";
        WhereClause += " and TrangThai = 1";
        string strHTML = "";

        TList<LoaiMatHang> Tlist_LoaiMatHang = DataRepository.LoaiMatHangProvider.GetPaged(WhereClause, "Id DESC", 0, 0, out ToTalRows);
        if (Tlist_LoaiMatHang.Count > 0)
        {
            for (int i = 0; i < Tlist_LoaiMatHang.Count; i++)
            {
                strHTML += "<li>";
                strHTML += "<a href='" + Tlist_LoaiMatHang[i].Id + "_" + GetTieuDe(Tlist_LoaiMatHang[i].TenMatHang) + "_MatHang.aspx'>";
                strHTML += Tlist_LoaiMatHang[i].TenMatHang;
                strHTML += "</a>";
                strHTML += "</li>";
                idLoaiMH.Text = strHTML;
            }
        }
    }

    protected void Bind_Data_MatHang(int idLoaiMH, int idSubMH, int PageNo)
    {
        try
        {
            int ToTalRows = 0;
            string WhereClause = "1=1";
            DataSet dsMathang = DataRepository.Provider.ExecuteDataSet("MatHang_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize);
            datalistMathang.DataSource = dsMathang;
            datalistMathang.DataBind();
            try
            {
                ToTalRows = Convert.ToInt32(dsMathang.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {

            }
            Pager1.ItemCount = ToTalRows;
            if (ToTalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }
        catch (Exception)
        {

        }
    }

    /// <summary>
    /// Thuc hien bindding du lieu dua tren LoaiMatHang
    /// </summary>
    /// <param name="idLoaiMH"></param>
    protected void Bind_Data_MatHang_DK(int idLoaiMH, int PageNo)
    {

        int ToTalRows = 0;
        string WhereClause = "1=1";
        string WhereClause1 = "1=1";
        WhereClause += " and IdLoaiMatHang = " + idLoaiMH;

        LoaiMatHang ent_loaiMh = DataRepository.LoaiMatHangProvider.GetById(idLoaiMH);
        ltrTieude.Text = ent_loaiMh.TenMatHang;

        TList<MatHang> Tlist_MatHang = new TList<MatHang>();

        // Lay danh sach cac SubMatHang
        TList<SubMatHang> Tlist_SubMathang = DataRepository.SubMatHangProvider.GetPaged(WhereClause, "Id DESC", 0, 0, out ToTalRows);

        if (Tlist_SubMathang.Count > 0)
        {
            for (int i = 0; i < Tlist_SubMathang.Count; i++)
            {
                // Lay danh sach cac MatHang co ma SubMatHang tuong ung
                WhereClause1 = "1=1"; // reset lai dieu kien
                WhereClause1 += " and IdSubMatHang = " + Tlist_SubMathang[i].Id;
                //WhereClause1 += " and TrangThai = 1";
                TList<MatHang> tmp = DataRepository.MatHangProvider.GetPaged(WhereClause1, "Id DESC", 0, 0, out ToTalRows);

                // Neu ton tai du lieu
                if (tmp.Count > 0)
                {
                    for (int j = 0; j < tmp.Count; j++)
                    {
                        Tlist_MatHang.Add(tmp[j]);
                    }
                }
            }

            // Khi co duoc du lieu roi thi thuc hien Bindding du lieu vao
            try
            {
                ToTalRows = Tlist_MatHang.Count;
            }
            catch (Exception)
            {

            }
            Pager1.ItemCount = ToTalRows;
            if (ToTalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
            //
            datalistMathang.DataSource = Tlist_MatHang;
            datalistMathang.DataBind();
        }
    }

    /// <summary>
    /// Phuong thuc chuyen tieu de co dau thanh khong dau de dua vao link
    /// </summary>
    /// <param name="Char"></param>
    /// <returns></returns>
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    /// <summary>
    /// Format cat chuoi
    /// </summary>
    /// <param name="str"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        if (Request.Params["Id"] != null)
        {
            Bind_Data_MatHang_DK(Int32.Parse(Request.Params["Id"]), currnetPageIndex);
        }
        else
        {
            Bind_Data_MatHang(0, 0, currnetPageIndex);
        }
    }


}